﻿
$(document).ready(function () {

	
	$(".for_popup").fancybox({
		'padding'			: 0,
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.4,
        'onComplete'	:	function() {
            var str = document.location.href;
            if(str.indexOf('login') ==-1) {
                $("#id_login_form").attr("action","/accounts/login/");
            }
        }
	});
    $("a.showcase").fancybox();

	// search
	$('#search_q').focus(function () {
		$(this).parents('.search_block').addClass('search_block_active');	
	});
	$('.where_to_look input').change(function () {
		$(this).parents('.item').siblings('.item_current').removeClass('item_current');
		$(this).parents('.item').addClass('item_current');		
	});
	
	
	// region select
	$('.region_select .current_val').click(function () {
		$(this).siblings('.selected_list').show();	
	});
	
	
	// side menu
	$('.side_menu .item_h').click(function () {
		$(this).parents('.item').toggleClass('item_current');
	});


	// file input
	$('.file_button .input').change(function () {
		var val = $(this).attr('value');
		$(this).parents('.file_button').siblings('.text_input').val(val);
	});


	
	
	// settings privacy val_select
	$('.privacy_list a.current_val').click(function () {
		$(this).siblings('.val_select').show();
		$(this).parents('dd').addClass('dd_active').siblings('dd').removeClass('dd_active');
		return false;
	});
	$('.privacy_list .val_select input').change(function () {
		var val = $(this).siblings('.val').html();
		$(this).parents('.val_select').siblings('a.current_val').html(val);
		$(this).parents('.item').addClass('item_current').siblings('.item').removeClass('item_current');
		$(this).parents('.val_select').hide();	
	});
	
	
	
	// hide active elements
	$(document).mousedown (function(event) {
		// hide search popup
		if (!$(event.target).parents("#search_block").length) {
			$('#search_block').removeClass('search_block_active');	
		}
		// hide settings privacy val_select
		if (!$(event.target).parents(".privacy_list .val_select").length) {
			$('.privacy_list .val_select').hide();	
		}
		// hide region selected list
		if (!$(event.target).parents(".region_select .selected_list").length) {
			$('.region_select .selected_list').hide();	
		}
    });
    $("#id_send_message").click(function () {
        var usr = $("#id_recipient").val();
        var txt = $("#id_body").val();
        if (txt!="")
        {
            $.post('/post_message_ajax/', {user_to:usr, message_text:txt}, function(res) {
                //$(".list_item:last").after(res);
                $("#id_body").val("");

            });
        }
	});

    $('#id_text').keypress(function(e){
        if(e.which == 13)
        {
            $('form#id_wall_post_form').submit();
        }
    });

    $('#id_subj').keypress(function(e){
        if(e.which == 13)
        {
            $('form#id_start_discussion_form').submit();
        }
    });
});









